﻿@using SelfMadeMillionaire.Core.Enums
@using SelfMadeMillionaire.Web.Infrastructure
@using SelfMadeMillionaire.Web.Infrastructure.Formatters
@using SelfMadeMillionaire.Web.Infrastructure.ViewModels.MailingList
@using PagedList.Mvc
@model MailingListViewModel

	<div class="table-responsive">
		@if (Model.Subscribers.Any())
  {
			<table class="table table-bordered tablesorter">
				<thead>
					<tr>
						<th><a href="#">First Name</a></th>
						<th><a href="#">Last Name</a></th>
						<th><a href="#">Email</a></th>
						<th width="60px;"><a href="#">SMM</a></th>
						<th width="130px;"><a href="#">Subscription Level</a></th>
						<th width="60px;"><a href="#">MMM</a></th>
						<th width="80px;"><a href="#">Start Date</a></th>
						<th width="80px;"><a href="#">End Date</a></th>
						<th width="70px;"><a href="#">Status</a></th>
						<th width="350px;">Action</th>
					</tr>
				</thead>
				<tbody>
					@foreach (var item in Model.Subscribers)
	 {
						<tr>
                            @{bool isSmm = (!item.SmmUnsubscribed);}
							<td>@(item.FirstName.Length > 20 ? item.FirstName.Substring(0,20) : item.FirstName)</td>
							<td>@(item.LastName.Length > 20 ? item.LastName.Substring(0,20) : item.LastName)</td>
							<td>@item.Email</td>
							<td align="center">@isSmm</td>
							<td align="center">
                                @if (isSmm) 
                                {
                                            <text>@((SubscriptionType?)item.SubscriptionLevel)</text>
                                }
							</td>
                                
							<td align="center">@(!item.MmmUnsubscribed)</td>
							<td align="center">@(item.StartDate.HasValue ? item.StartDate.Value.ToString("MM/dd/yyyy") : null)</td>
							<td align="center">@(item.EndDate.HasValue ? item.EndDate.Value.ToString("MM/dd/yyyy") : null)</td>
							<td align="center">@Enum.Parse(typeof(StatusType), item.Status.ToString())</td>
							<td align="center">
                                <a href="#" class="edit-link" data-id="@item.ID">Edit</a> 
								| <a href="#" class="remove-lnk" data-id="@item.ID">Delete</a>
                                | <a href="#" class="pass-lnk" data-id="@item.ID">Change password</a>
                                | <a href="#" class="subscription-lnk" data-id="@item.ID">Apply Payment</a>
                                | <a href="#" class="edit-subscription-lnk" data-id="@item.ID">Subscription</a>
							</td>
						</tr>
	 }
				</tbody>
			</table>
	  @Html.PagedListPager(Model.Subscribers, page => Url.Action("Index", new { page, firstName = Model.FirstName, lastName = Model.LastName, selectedProduct = Model.ProductTypes.SelectedValue, email = Model.Email, paget = Model.PageT, sort = Model.SortOrder }))
  }
  else
  {
			<p>No records found.</p>
  }
	</div>

<script type="text/javascript">
	$(function () {
		$("#mailing-list table").tablesorter({
		    headers: {
		        9: { sorter: false }
		    }
		});

		$(".remove-lnk").click(function () {
			if (confirm('Are you sure you want to delete this item?')) {
				$.ajax({
					url: '@Url.Action("Delete", "MailingList")',
					type: "POST",
					data: { id: $(this).attr('data-id') },
					cache: false,
					success: function () {
						refresh();
					}
				});
			}
			return false;
		});

		$(".pass-lnk").click(function () {

			$.ajax({
				url: '@Url.Action("ChangePassword", "MailingList")',
				data: { id: $(this).attr('data-id') },
				cache: false,
				success: function (result) {
					$("#edit-modal-container").html(result);
					$("#edit-modal-container").modal();
				}
			});

			return false;
		});

	    $(".edit-link").click(function () {

	        $.ajax({
	            url: '@Url.Action("Edit", "MailingList")',
			    data: { id: $(this).attr('data-id') },
			    cache: false,
			    success: function (result) {
			        $("#edit-modal-container").html(result);
			        $("#edit-modal-container").modal();
			    }
			});

            return false;
	    });

	    $("#btnAddNew").click(function () {

	        $.ajax({
	            url: '@Url.Action("Edit", "MailingList")',
	            cache: false,
	            success: function (result) {
	                $("#edit-modal-container").html(result);
	                $("#edit-modal-container").modal();
	            }
	        });

            return false;
        });

	    $(".subscription-lnk").click(function () {

	        $.ajax({
	            url: '@Url.Action("SubscriptionInfo", "MailingList")',
	            data: { id: $(this).attr('data-id') },
	            cache: false,
	            success: function (result) {
	                $("#edit-modal-container").html(result);
	                $("#edit-modal-container").modal();
	            }
	        });

            return false;
	    });

	    $(".edit-subscription-lnk").click(function () {

	        $.ajax({
	            url: '@Url.Action("EditSubscriptionInfo", "MailingList")',
	            data: { id: $(this).attr('data-id') },
	            cache: false,
	            success: function (result) {
	                $("#edit-modal-container").html(result);
	                $("#edit-modal-container").modal();
	            }
	        });

            return false;
	    });
        
	    var sortRefresh = true;
	    @if(!string.IsNullOrEmpty(Model.SortOrder))
        {
            <text>
	        sortRefresh = false;
	        setInitSort("mailing-list", '@(Model.SortOrder)');
            </text>
        }

	    $("#mailing-list table").on("sortEnd", function (event) {
	        $("#SortOrder").val(event.target.config.sortList);
	        if (sortRefresh)
	            refresh();
	        else
	            sortRefresh = true;

	    });

	});	

</script>